package com.jiyun;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")
public class SpringdataRedisDemo_Zset {

    @Autowired
    RedisTemplate redisTemplate;

    /*
    zset类型增删改查  key—TreeSet  有序，不可重复
     */

    @Test
    public void add(){
     /*redisTemplate.boundZSetOps("zbj").add("张三",200);
     redisTemplate.boundZSetOps("zbj").add("李四",300);
     redisTemplate.boundZSetOps("zbj").add("王五",100);
     redisTemplate.boundZSetOps("zbj").add("赵六",1000);*/
     redisTemplate.boundZSetOps("zbj").add("田七",0);
    }

    @Test
    public void query(){
        Set zbj = redisTemplate.boundZSetOps("zbj").range(0, -1);
        for (Object o : zbj) {
            System.out.println(o);
        }

        System.out.println("----------------------");

        Set<ZSetOperations.TypedTuple> zbj1 = redisTemplate.boundZSetOps("zbj").rangeWithScores(0, -1);
        for (ZSetOperations.TypedTuple typedTuple : zbj1) {
            System.out.println(typedTuple.getValue() + "---" + typedTuple.getScore());
        }

        System.out.println("----------------------");

        Set<ZSetOperations.TypedTuple> zbj2 = redisTemplate.boundZSetOps("zbj").reverseRangeWithScores(0, 2 );
        for (ZSetOperations.TypedTuple typedTuple : zbj2) {
            System.out.println(typedTuple.getValue() + "---" + typedTuple.getScore());
        }
    }

    @Test
    public void addSocre(){
        redisTemplate.boundZSetOps("zbj").incrementScore("王五",2000);
    }


    @Test
    public void delItem(){
        redisTemplate.boundZSetOps("zbj").remove("田七");
    }
    @Test
    public void del(){
        redisTemplate.delete("zbj");
    }



}
